package com.book.shop.Webservice;

import com.book.shop.model.Book;
import com.book.shop.model.Order;
import com.book.shop.model.OrderItems;
import com.book.shop.model.User;
import com.book.shop.repository.OrderRepository;
import com.book.shop.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Service
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private UserRepository userRepository;
    private Order order = new Order();
    //添加图书到订单
    public void AddBookToOrder(Book book)
    {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal orderItemPrice = new BigDecimal(0);
        int isFoundDuplidatedBook = 0;
        for (OrderItems orderItem : order.getBooks()) {
            if (orderItem.getBook().equals(book)) {
                isFoundDuplidatedBook = 1;
                orderItem.setQuantity(orderItem.getQuantity()+1);
                break;
            }
        }
        if(0==isFoundDuplidatedBook){
            order.addBook(book,1L);
        }
        orderItemPrice = book.getBookPrice();
        if(order.getOrderPrice()==null){
            totalPrice=orderItemPrice;
        }else{
            totalPrice = order.getOrderPrice().add(orderItemPrice);
        }
        order.setOrderPrice(totalPrice);
    }
    //从订单中删除图书
    public void removeBookFromOrder(Book book) {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal orderItemPrice = new BigDecimal(0);
        OrderItems foundOrderItem =null;
        for (OrderItems orderItem : order.getBooks()) {
            if (orderItem.getBook().equals(book)) {
                foundOrderItem=orderItem;
                orderItemPrice = new BigDecimal(orderItem.getQuantity()).multiply(book.getBookPrice());
                break;
            }
        }
        if(foundOrderItem !=null){
            order.getBooks().remove(foundOrderItem);
        }
//        order.removeBook(book);
        totalPrice = order.getOrderPrice().subtract(orderItemPrice);
        order.setOrderPrice(totalPrice);
    }
    //生成订单
    public void createOrder(String userName){
        User user = userRepository.findByUserName(userName);
        order.setUser(user);
        order.setOrderDate(Calendar.getInstance().getTime());
        orderRepository.save(order);
    }
    //查询所有订单
    public List<Order> findAllOrders(){
        return orderRepository.findAll();
    }
    //查询订单状态
    public List<Order> findByOrderStatus(boolean orderStatus){
        return orderRepository.findByOrderStatus(orderStatus);
    }
    //按订单号查询
    public Order findOrderById(Long orderId){
        return orderRepository.findById(orderId).get();
    }
    //编辑订单
    public void updateOrder(Order order){
        orderRepository.save(order);
    }
    //删除订单
    public void deleteOrder(Order order){
        orderRepository.delete(order);
    }
    //按用户名查询订单
    public List<Order> findByUserName(String userName){
        User user =userRepository.findByUserName(userName);
        return orderRepository.findByUser(user);
    }
    //查询订单对象
    public Order getOrder(){
        return order;
    }
    //消除订单对象
    public Order clearOrder(){
        order = new Order();
        return order;
    }
}
